package me.stone.stanimeclient.core.ui.player

import androidx.media3.common.AudioAttributes
import androidx.media3.common.DeviceInfo
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Metadata
import androidx.media3.common.PlaybackException
import androidx.media3.common.PlaybackParameters
import androidx.media3.common.Player
import androidx.media3.common.Timeline
import androidx.media3.common.TrackSelectionParameters
import androidx.media3.common.Tracks
import androidx.media3.common.VideoSize
import androidx.media3.common.text.CueGroup
import androidx.media3.common.util.UnstableApi
import timber.log.Timber

abstract class DefaultPlayerListener : Player.Listener {

    override fun onRenderedFirstFrame() {
        Timber.d("onRenderedFirstFrame")
    }

    override fun onAudioAttributesChanged(audioAttributes: AudioAttributes) {
        Timber.d("onAudioAttributesChanged -> $audioAttributes")
    }

    @UnstableApi
    override fun onAudioSessionIdChanged(audioSessionId: Int) {
        Timber.d("onAudioSessionIdChanged -> $audioSessionId")
    }

    override fun onAvailableCommandsChanged(availableCommands: Player.Commands) {
        Timber.d("onAvailableCommandsChanged -> $availableCommands")
    }

    override fun onCues(cueGroup: CueGroup) {
        Timber.d("onCues -> $cueGroup")
    }

    override fun onDeviceInfoChanged(deviceInfo: DeviceInfo) {
        Timber.d("onDeviceInfoChanged -> $deviceInfo")
    }

    override fun onIsLoadingChanged(isLoading: Boolean) {
        Timber.d("onIsLoadingChanged -> $isLoading")
    }

    override fun onIsPlayingChanged(isPlaying: Boolean) {
        Timber.d("onIsPlayingChanged -> $isPlaying")
    }

    override fun onMaxSeekToPreviousPositionChanged(maxSeekToPreviousPositionMs: Long) {
        Timber.d("onMaxSeekToPreviousPositionChanged -> $maxSeekToPreviousPositionMs")
    }

    override fun onMediaMetadataChanged(mediaMetadata: MediaMetadata) {
        Timber.d("onMediaMetadataChanged -> $mediaMetadata")
    }

    @UnstableApi
    override fun onMetadata(metadata: Metadata) {
        Timber.d("onMetadata -> $metadata")
    }

    override fun onPlaybackParametersChanged(playbackParameters: PlaybackParameters) {
        Timber.d("onPlaybackParametersChanged -> $playbackParameters")
    }

    override fun onPlaybackStateChanged(playbackState: Int) {
        super.onPlaybackStateChanged(playbackState)
        Timber.d("onPlaybackStateChanged -> $playbackState")
    }

    override fun onPlaybackSuppressionReasonChanged(playbackSuppressionReason: Int) {
        Timber.d("onPlaybackSuppressionReasonChanged -> $playbackSuppressionReason")
    }

    override fun onPlayerError(error: PlaybackException) {
        Timber.d("onPlayerError -> $error")
    }

    override fun onPlayerErrorChanged(error: PlaybackException?) {
        Timber.d("onPlayerErrorChanged -> $error")
    }

    override fun onPlaylistMetadataChanged(mediaMetadata: MediaMetadata) {
        Timber.d("onPlaylistMetadataChanged -> $mediaMetadata")
    }

    override fun onRepeatModeChanged(repeatMode: Int) {
        Timber.d("onRepeatModeChanged -> $repeatMode")
    }

    override fun onSeekBackIncrementChanged(seekBackIncrementMs: Long) {
        Timber.d("onSeekBackIncrementChanged -> $seekBackIncrementMs")
    }

    override fun onSeekForwardIncrementChanged(seekForwardIncrementMs: Long) {
        Timber.d("onSeekForwardIncrementChanged -> $seekForwardIncrementMs")
    }

    override fun onShuffleModeEnabledChanged(shuffleModeEnabled: Boolean) {
        Timber.d("onShuffleModeEnabledChanged -> $shuffleModeEnabled")
    }

    override fun onSkipSilenceEnabledChanged(skipSilenceEnabled: Boolean) {
        Timber.d("onSkipSilenceEnabledChanged -> $skipSilenceEnabled")
    }

    override fun onTrackSelectionParametersChanged(parameters: TrackSelectionParameters) {
        Timber.d("onTrackSelectionParametersChanged -> $parameters")
    }

    override fun onTracksChanged(tracks: Tracks) {
        Timber.d("onTracksChanged -> $tracks")
    }

    override fun onVideoSizeChanged(videoSize: VideoSize) {
        Timber.d("onVideoSizeChanged -> w: ${videoSize.width}, h: ${videoSize.height}")
    }

    override fun onVolumeChanged(volume: Float) {
        Timber.d("onVolumeChanged -> $volume")
    }

    override fun onDeviceVolumeChanged(volume: Int, muted: Boolean) {
        Timber.d("onDeviceVolumeChanged -> volume: $volume, muted: $muted")
    }

    override fun onEvents(player: Player, events: Player.Events) {
        Timber.apply {
            d("onEvents >>>")
            for (i in 0 ..< events.size()) {
                d("i: ${events[i]}")
            }
            d("onEvents <<<")
        }
    }

    override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
        Timber.d("onMediaItemTransition -> mediaItem: $mediaItem, reason: $reason")
    }

    override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
        Timber.d("onPlayWhenReadyChanged -> playWhenReady: $playWhenReady, reason: $reason")
    }

    override fun onSurfaceSizeChanged(width: Int, height: Int) {
        Timber.d("onSurfaceSizeChanged -> width: $width, height: $height")
    }

    override fun onTimelineChanged(timeline: Timeline, reason: Int) {
        Timber.d("onTimelineChanged -> timeline: $timeline, reason: $reason")
    }

    override fun onPositionDiscontinuity(
        oldPosition: Player.PositionInfo,
        newPosition: Player.PositionInfo,
        reason: Int
    ) {
        Timber.d("onPositionDiscontinuity -> oldPos: $oldPosition, newPos: $newPosition, reason: $reason")
    }

}